package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.resource.MessageConstant;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 批量导入预约数据
     * @param file
     * @return
     */
    @RequestMapping("/importOrderSettings.do")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file){

        try {
            //判断文件是否为空
            POIUtils.checkFile(file);
            //读取execl表格中数据
            List<String[]> readExcel = POIUtils.readExcel(file);
            //创建一个新集合用来存放每一行的数据
            List<OrderSetting> orderSettingList=new ArrayList<>();
            //循环遍历readExcel,将每一行的数据封装到OrderSetting实体中
            for (String[] strings : readExcel) {
                OrderSetting orderSetting=new OrderSetting();
                orderSetting.setOrderDate(DateUtils.parseString2Date(
                        strings[0],"yyyy/MM/dd"));
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                //将orderSetting实体放到orderSettingList集合中
                orderSettingList.add(orderSetting);
            }
            //将预约的数据放到数据库中
            orderSettingService.importOrderSettings(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    /**
     * 获取预约数据，进行前台页面的展示
     * @param year
     * @param month
     * @return
     */
    @RequestMapping("/findOrderSettingByDate.do")
    public Result findOrderSettingByDate(@RequestParam("year") Integer year,@RequestParam("month") Integer month){
        try {
            //调用service层方法,将当前的年和月传递过去
            List listMap=orderSettingService.findOrderSettingByDate(year,month);
            return new Result(true,MessageConstant.QUERY_ORDERSETTING_SUCCESS,listMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
    }

    /**
     * 更新预约数
     * @param dayInfo
     * @param value
     * @return
     */
    @RequestMapping("/updateOrderSettingData.do")
    public Result updateOrderSettingData(@RequestParam("dayInfo") String dayInfo,@RequestParam("value") Integer value){
        try {

           orderSettingService.updateOrderSettingData(dayInfo,value);
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
    }

}
